package com.example.yls.demoa;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ListView listVStudent;
    private Button buttonAdd;
    private Button buttonSearch;
    private ArrayList<Student> studentArrayList=new ArrayList<>();
    private StudentAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listVStudent = findViewById(R.id.lv_stu);
        buttonAdd = findViewById(R.id.btn_add);
        buttonSearch = findViewById(R.id.btn_search);

//        studentArrayList.add(new Student("171001","lisa", 18));
//        studentArrayList.add(new Student("171002","mical", 19));
//        studentArrayList.add(new Student("171003","liming", 20));.

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });

        // 第一个问题  数据从哪里来
        studentArrayList = readDataFromDB();

        adapter = new StudentAdapter(MainActivity.this, studentArrayList);
        listVStudent.setAdapter(adapter);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == 2001){
            String no = data.getStringExtra("NO");
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE",0);
            Student student = new Student(no, name, age);

            addToStudentDB(student);
        }

    }

    private void addToStudentDB(Student student) {
        String path = getFilesDir().getAbsolutePath() + File.separator + "stu.db";
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(path, null);

        ContentValues values = new ContentValues();
        values.put("stuno", student.getStunumber());
        values.put("name", student.getName());
        values.put("age", student.getAge());
        db.insert("student", null, values);

        studentArrayList = readDataFromDB();
        adapter.changeData(studentArrayList);
    }

    private ArrayList<Student> readDataFromDB() {
        ArrayList<Student>  studentArrayList = new ArrayList<>();

        String path = getFilesDir().getAbsolutePath() + File.separator + "stu.db";
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(path, null);

        String sql = "create table if not exists student (stuno varchar(20), name varchar(20), age int)";
        db.execSQL(sql);

        Cursor cursor = db.query("student", null,null,null,null,null,null);

        if(cursor == null || cursor.getCount() == 0){
            return studentArrayList;
        }

        for(cursor.moveToFirst(); !cursor.isAfterLast();cursor.moveToNext()){
            String stuno = cursor.getString(0);
            String stuname = cursor.getString(1);
            int stuAge = cursor.getInt(2);
            studentArrayList.add(new Student(stuno, stuname, stuAge));
        }

        db.close();
        return studentArrayList;


    }
}
